package nix.spring.jdbc;

import java.beans.PropertyVetoException;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@RunWith(SpringJUnit4ClassRunner.class)// 自动创建容器
@ContextConfiguration("classpath:nix/spring/jdbc/applicationContext.xml")// 指定配置文件
public class Demo1 {

	@Resource(name="userDao")
	private UserDao userDao;
	
	@Test
	public void fun1() throws PropertyVetoException {
		
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql:///sd");
		dataSource.setUser("root");
		dataSource.setPassword("race");
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		
		String sql = "insert into t_user values (null, 'wangWu', '123')";
		jt.update(sql);
	}
	
	@Test
	public void fun2() {

		User u = new User();
		u.setName("zhaoLiu");
		u.setPassword("123");
		userDao.save(u);
	}
	
	@Test
	public void fun3() {

		User u = new User();
		u.setId(6);
		u.setName("zhaoLiu2");
		u.setPassword("1234");
		userDao.update(u);
	}
	
	@Test
	public void fun4() {

		User user = userDao.getById(6);
		System.out.println(user);
	}
	
	@Test
	public void fun5() {

		int count = userDao.getTotalCount();
		System.out.println(count);
	}
	
	@Test
	public void fun6() {

		List<User> list = userDao.getAll();
		System.out.println(list);
	}
	
	@Test
	public void fun7() {

		userDao.delete(6);
	}
}
